package com.vencent.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.thymeleaf.spring3.SpringTemplateEngine;
import org.thymeleaf.spring3.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

@Configuration
@ComponentScan
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
	/**
	 * where is choose the default path can be filter ?.
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	/**
	 * ThymeleafView  thymeleaf view resolver  step 2
	 * If you use jsp and html, you need to divide the subfile to distinguish using what viewResolver .
	 * be careful you need to modify three point: controller 's return ;  viewResolver's viewNames; resolver's prefix
	 * @param templateEngine
	 * @return
	 */
	@Bean
	public ViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine){
		ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
		viewResolver.setTemplateEngine(templateEngine);
		viewResolver.setViewNames(new String[]{"/templates/*"});
		viewResolver.setOrder(0);
		return viewResolver;
	}
	
	/**
	 * TemplateEngine  Template engine :analysis & binding & render to response step 3
	 * @param templateResolver
	 * @return
	 */
	@Bean
	public SpringTemplateEngine templateEngine(TemplateResolver templateResolver){
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver);
		return templateEngine;
	}
	
	/**
	 * TemplateResolver template resolver : location the view step 1
	 * @return
	 */
	@Bean
	public TemplateResolver templateResolver(){
		TemplateResolver templateResolver = new ServletContextTemplateResolver();
		templateResolver.setPrefix("/WEB-INF/");
		templateResolver.setSuffix(".html");
		templateResolver.setTemplateMode("HTML5");
		return templateResolver;
	}
	
	@Bean
	public ViewResolver viewResolver(){
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setOrder(1);
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		/**
		 * you can use ${} to visit the Spring bean in JSP2.0
		 */
		resolver.setExposeContextBeansAsAttributes(true);
		
		/**
		 * you can use the Locale object and spring's information resource sources.
		 */
		resolver.setViewClass(JstlView.class);
		return resolver;
	}
}
